package com.railway.news.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLContentHelper {
	
	public static void deleteContent(String path)  throws Exception {
		
		File inputXml = new File(path);
		inputXml.delete();
	}
	
	
	public static void modifyContent(String path,String id, String content) throws Exception {

		File inputXml = new File(path);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(inputXml);
		String xpath = "/ROOT/Content[@id='" + id + "']";//查询属性type='ondDelete'的composite   
		List contentElements = document.selectNodes(xpath);
		Iterator iter = contentElements.iterator();

		while (iter.hasNext()) {
			Element element = (Element) iter.next();
			element.setText(content);

		}
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GBK"); // 指定XML编码        

		//         OutputFormat  format = new OutputFormat("   ",true,"GBK");   

		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileOutputStream(inputXml), format);
			writer.write(document);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}
    public static void createNewsContent(String savepath,String id, String content) throws Exception {
    	File inputXml = new File(savepath);
		//创建一个DocumentFactory duixiang    
		DocumentFactory factory = new DocumentFactory();
		//创建一个Document对象   
		Document document = factory.createDocument();

		//向Document对象中添加一个处理指令节点   
		//document.addProcessingInstruction("crazyit", "website=\"http://www.crazyit.org\"");   
		//向doc中添加根节点元素   
		Element root = document.addElement("ROOT");
		//采用循环的方式添加四个子元素   


			Element pcBook = root.addElement("Content");
			pcBook.addAttribute("id", id);

			pcBook.setText(content);

		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GBK"); // 指定XML编码        

		//        OutputFormat  format = new OutputFormat("   ",true,"GBK");   
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileOutputStream(inputXml), format);
			writer.write(document);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
    }
	public static void addConent(String id, String content) throws Exception {
		String filePath = "C:/Contents.xml";
		File inputXml = new File(filePath);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(inputXml);

		Element rootElement = document.getRootElement();
		Element contentElement = rootElement.addElement("Content");
		contentElement.addAttribute("id", id);
		contentElement.addText(content);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GBK"); // 指定XML编码        

		//          OutputFormat  format = new OutputFormat("   ",true,"GBK");   
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileOutputStream(inputXml), format);
			writer.write(document);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public static String queryContent(String path,String id) throws Exception {
		File inputXml = new File(path);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(inputXml);
		String xpath = "/ROOT/Content[@id='" + id + "']";//查询属性type='ondDelete'的composite   
		List contentElements = document.selectNodes(xpath);
		Iterator iter = contentElements.iterator();

		while (iter.hasNext()) {
			Element element = (Element) iter.next();
			String value = element.getText();
			return value;
		}
		return null;
	}

	public void generateDocument() throws Exception {
		File inputXml = new File("C:/Contents.xml");
		//创建一个DocumentFactory duixiang    
		DocumentFactory factory = new DocumentFactory();
		//创建一个Document对象   
		Document document = factory.createDocument();

		//向Document对象中添加一个处理指令节点   
		//document.addProcessingInstruction("crazyit", "website=\"http://www.crazyit.org\"");   
		//向doc中添加根节点元素   
		Element root = document.addElement("ROOT");
		//采用循环的方式添加四个子元素   
		for (int i = 0; i < 4; i++) {

			Element pcBook = root.addElement("Content");
			pcBook.addAttribute("id", "id" + i);

			pcBook.setText("内容" + i);

		}
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GBK"); // 指定XML编码        

		//        OutputFormat  format = new OutputFormat("   ",true,"GBK");   
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileOutputStream(inputXml), format);
			writer.write(document);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public static void main(String[] argv) throws Exception {
		XMLContentHelper dom4j = new XMLContentHelper();
		dom4j.generateDocument();
		dom4j.addConent("id1111", "我是内容 ");

//		 System.out.println(	dom4j.queryContent("id1111"));
//		 dom4j.modifyContent("id1111", "我是新内容");
//		 System.out.println(	dom4j.queryContent("id1111"));
	}
}
